package engine;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import queries.Document;

/**
 * Representation of indexed document
 * 
 * @author Vika
 * @author ak2765
 */
public final class IndexedDocument {

	private final int documentId;
	
	private final Document document;
	
	private final List<String> titleWords = new LinkedList<String>();
	
	private final List<String> descriptionWords = new LinkedList<String>();

	private final Map<String,Integer> titleFrequencies = new HashMap<String,Integer>();
	
	private final Map<String,Integer> descriptionFrequencies = new HashMap<String,Integer>();
	
	/**
	 * @param documentId
	 * @param document
	 * @param titleTokenizer
	 * @param descriptionTokenizer
	 */
	public IndexedDocument(int documentId, Document document,
			StringTokenizer titleTokenizer, StringTokenizer descriptionTokenizer) {

		this.documentId = documentId;
		this.document = document;
		
		while (titleTokenizer.hasMoreTokens()) {
			String word = titleTokenizer.nextToken();
			titleWords.add(word);
			
			if (this.titleFrequencies.containsKey(word)) {
				this.titleFrequencies.put(word, this.titleFrequencies.get(word) + 1);
			} else {
				this.titleFrequencies.put(word, 1);
			}
		}

		while (descriptionTokenizer.hasMoreTokens()) {
			String word = descriptionTokenizer.nextToken();
			
			descriptionWords.add(word);
			
			if (this.descriptionFrequencies.containsKey(word)) {
				this.descriptionFrequencies.put(word, this.descriptionFrequencies.get(word) + 1);
			} else {
				this.descriptionFrequencies.put(word, 1);
			}
			
		}
	}

	/**
	 * @param word
	 * @return
	 */
	public int getTitleFrequencyForWord(String word) {
		if (false == this.titleFrequencies.containsKey(word)) {
			return 0;
		}
		return this.titleFrequencies.get(word);
	}

	/**
	 * @param word
	 * @return
	 */
	public int getDescriptionFrequencyForWord(String word) {
		if (false == this.descriptionFrequencies.containsKey(word)) {
			return 0;
		}

		return this.descriptionFrequencies.get(word);
	}
	
	public int getDocumentId() {
		return documentId;
	}

	public Document getDocument() {
		return document;
	}

	public List<String> getTitleWords() {
		return new LinkedList<String>(titleWords);
	}

	public List<String> getDescriptionWords() {
		return new LinkedList<String>(descriptionWords);
	}


}
